<?php

class myUser extends sfGuardSecurityUser
{

	public function addViviendaToVisited($id){
		$idsVisited = $this->getAttribute('vivienda_visited', array());
		if (in_array($id, $idsVisited)){
		$index=array_search($id, $idsVisited);
			unset($idsVisited[$index]);
		}
		$idsVisited[time()] = $id;
		ksort($idsVisited);
		$this->setAttribute('vivienda_visited', $idsVisited);
	}
	
	public function numberVisited(){
		$ids = $this->getAttribute('vivienda_visited', array());
		return count($ids);
	}
	
	public function getVisitedString(){
		$strVisited = "";
		$ids = $this->getAttribute('vivienda_visited', array());
		foreach ($ids as $key => $value) {
			$strVisited .= $key.'-'.$value."  ";
		}
		return $strVisited;
	}
	
	public function getVisitedResult(){
		$visitedResult = null;
		if($this->numberVisited()>0){
			$ids = $this->getAttribute('vivienda_visited', array());
			$q = Doctrine_Query::create()
	 ->select('v.*, v.precioMin as minPrecioEuros')
	  ->from('ViviendasAlquileres v')
	  ->whereIn('v.id', $ids)
	  ;
		$visitedResult = $q->fetchArray();	
		usort($visitedResult, array("ViviendasAlquileres", "cmp_obj_Visited"));
		return $visitedResult;
		}
		
	}
	
	public function addViviendaToFavorites($id){
		//Si esta autenticado se guarda en BD
		if ($this->isAuthenticated()){
			$favUserVivienda = new FavUserVivienda();
			$favUserVivienda->setSfGuardUserId($this->getGuardUser()->getId());
			$favUserVivienda->setViviendaId($id);
			$favUserVivienda->replace();
		}
		//Si no esta autenticado se guarda en sesion
		else{
			$idsFavorites = $this->getAttribute('vivienda_favorites', array());
			if (!in_array($id, $idsFavorites)){
			  //array_unshift($ids, $id);
			  $idsFavorites[time()] = $id;
			  ksort($idsFavorites); 
			  $this->setAttribute('vivienda_favorites', $idsFavorites);
			}
		}
	}
	
	
	public function deleteViviendaFavorites($id){
		if ($this->isAuthenticated()){
			$deleted = Doctrine_Query::create()
			  ->delete()
			  ->from('FavUserVivienda')
			  ->where('sf_guard_user_id = ?', $this->getGuardUser()->getId())
			  ->andWhere('vivienda_id = ?', $id)
			  ->execute();
		}
		else{
			$ids = $this->getAttribute('vivienda_favorites', array());
			if (in_array($id, $ids))
			{
				$index=array_search($id, $ids);
				unset($ids[$index]);
				$this->setAttribute('vivienda_favorites', $ids);
			}
		}
	}
	
	public function checkFavorites($id){
		$ids = $this->getAttribute('vivienda_favorites', array());
	 
		if (!in_array($id, $ids))
		{
		  return false;
		}
		return true;
	}
	
	public function numberFavorites(){
		if ($this->isAuthenticated()){
			$q = Doctrine_Query::create()
			  ->select('f.*')
			  ->from('FavUserVivienda f')
			  ->where('f.sf_guard_user_id = ?', $this->getGuardUser()->getId());
			  $ids = $q->execute();
		}
		else{
			$ids = $this->getAttribute('vivienda_favorites', array());
		}
		return count($ids);
	}
	
	public function getFavoritesString(){
		$strFavorites = "";
		$ids = $this->getAttribute('vivienda_favorites', array());
		foreach ($ids as $key => $value) {
			$strFavorites .= $key.'-'.$value."  ";
		}
		return $strFavorites;
	}
	
	public function getFavoritesResult(){
			if($this->hasAttribute('vivienda_favorites') && $this->numberFavorites()>0){
			$ids = $this->getAttribute('vivienda_favorites', array());
			$q = Doctrine_Query::create()
	 ->select('v.*, MIN(d.precio_Euros) as minPrecioEuros')
	  ->from('ViviendasAlquileres v')
	  ->whereIn('v.id', $ids)
	  ->leftJoin('v.AlquileresPrecios d')
	  ->groupBy('v.id')
	  //->addHaving('MIN(d.precio_Euross')
	  ;
	$favoritesResult = $q->fetchArray();	
		if(count($favoritesResult)>1){
			usort($favoritesResult, array("ViviendasAlquileres", "cmp_obj"));
		}
			return $favoritesResult;
		}


		
	}

	/*
		public function getFavoritesResult(){
	//Usuario autenticado
	if($this->isAuthenticated()){
		$qi = Doctrine_Query::create()
		  ->select('f.vivienda_id')
		  ->from('FavUserVivienda f')
		  ->where('f.sf_guard_user_id = ?', $this->getGuardUser()->getId());
		  $idsDoc = $qi->execute();
		  $ids = array();
		  foreach($idsDoc as $id){
			$ids[] = $id->getViviendaId();
			
		  }
	
	}
	//Usuario no autenticado
	else{
		$ids = $this->getAttribute('vivienda_favorites', array());
	}
	
	if(count($ids)>0){
	//Obtener casas
	$qFav = Doctrine_Query::create()
	 ->select('v.*, MIN(d.precio_Euros) as minPrecioEuros')
	  ->from('ViviendasAlquileres v')
	  ->whereIn('v.id', $ids)
	  ->leftJoin('v.AlquileresPrecios d')
	  ->groupBy('v.id')
	  //->addHaving('MIN(d.precio_Euross')
	  ;
	  
	$this->favoritesResult = $qFav->execute();
		
	}
		
	}

	*/
	
	public function getLastLogin(){
		return $this->getLastLogin();
	}
}
